﻿using RestSharp;
using RestSharp.Deserializers;
using System;
using System.Collections.Generic;
using System.Net;

namespace ApiCore.TheMovieDB
{
    public class ApiCoreMovieDb : ApiCore
    {
        private const string initialUri = @"http://api.themoviedb.org/3/";

        public override List<FilmInfo> GetFilmsInfo(DateTime fromDate)
        {
            string apiKey = @"6cb510cff036b8c065bf87b63dc4e932";
            RestRequest request = new RestRequest("search/movie");
            request.AddHeader("Accept", "application/json");
            request.AddParameter("api_key", apiKey);
            request.AddParameter("query", "harry potter");
            request.Method = Method.GET;
            var client = new RestClient(@"http://api.themoviedb.org/3");
            client.AddHandler("application/json", new WatJsonDeserializer());
            var resp = client.Execute(request);
            resp.Content = @"{
         \"adult\":false,
         "backdrop_path":"/eZxahf9ATsv3oQPtvZPqSZkAO7V.jpg",
         "id":12444,
         "original_title":"Harry Potter and the Deathly Hallows: Part 1",
         "release_date":"2010-11-19",
         "poster_path":"/6nHrNTF3qOY19m07paTI8idEaFU.jpg",
         "popularity":2.6462,
         "title":"Harry Potter and the Deathly Hallows: Part 1",
         "vote_average":8.3,
         "vote_count":75
      }";
            JsonDeserializer des = new JsonDeserializer();
            var result = des.Deserialize<some>(resp);
            var anotherResult = des.Deserialize<some>(
            return null;
        }

        class some
        {
            [DeserializeAs(Name = "original_title")]
            public string Title { get; set; }
        }

        public override List<FilmInfo> GetFilmsInfo(DateTime fromDate, DateTime tillDate)
        {
            throw new NotImplementedException();
        }

        public override List<FilmInfo> GetFilmINfo(string filnTitle)
        {
            throw new NotImplementedException();
        }
    }

    public class WatJsonDeserializer : IDeserializer
    {
        #region IDeserializer Members

        public T Deserialize<T>(RestSharp.IRestResponse response)
        {
            if (response.Content == null)
                return default(T);

            // 2012-07-26 - attempt to handle some of the bad json content, eg Similar Movies for id 80271 containing null values
            //    amongst the other objects in the results array
            //    eg ...."vote_average":0.0,"vote_count":0},null,null,{"backdrop_path":null,"id":73736,"original_title":...
            //                                              ^^^^ ^^^^
            //    Causes "Object reference not set to an instance of an object" exception during deserialization
            while (response.Content.IndexOf("},null,") != -1)
            {
                response.Content = response.Content.Replace("},null,", "},");
            }

            while (response.Content.IndexOf("[null,") != -1)
            {
                response.Content = response.Content.Replace("[null,", "[");
            }

            var deserializer = new JsonDeserializer();
            var data = deserializer.Deserialize<T>(response);

            return data;
        }

        public string DateFormat { get; set; }
        public string Namespace { get; set; }
        public string RootElement { get; set; }

        #endregion
    }
}
